"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : range_range二级分区表:toast表
Create At   : 2022/2/23
Owner       : @daiguatutu
Description :
    1.创建表空间和二级分区表
    2.查询分区在系统表pg_class中对应relname
    3.插入数据
    4.更新数据,执行20次
    5.查询分区p_range_1_1的长度和对应的pg_toast数据
    6.清空表数据,设置存储模式external,插入新数据
    7.更新数据,执行20次
    8.查询分区p_range_1_1的长度和对应的pg_toast数据
    9.插入新数据
    10.更新数据,执行20次
    11.查询分区p_range_1_1的长度和对应的pg_toast数据
    12.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功,数据压缩和行外存储
    6.成功
    7.成功
    8.成功,数据未压缩和行外存储
    9.成功
    10.成功
    11.成功,数据未压缩和行外存储
    12.成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0291'
        self.ts_name = 'ts_subpartition_0291'

    def test_subpartition(self):
        text = '-----step1:创建表空间和二级分区表 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int ,col_2 text,col_3 varchar2 ( 30 ) " \
            f"not null ,col_4 int)tablespace {self.ts_name} " \
            f"partition by range (col_1) subpartition by range (col_2) " \
            f"( partition p_range_1 values less than(10) " \
            f"  ( subpartition p_range_1_1 values less than(5)," \
            f"    subpartition p_range_1_2 values less than(maxvalue)), " \
            f"  partition p_range_2 values less than(20) " \
            f"  ( subpartition p_range_2_1 values less than(5), " \
            f"    subpartition p_range_2_2 values less than(maxvalue)) " \
            f") enable row movement; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, msg, '执行失败:' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:查询分区在系统表pg_class中对应relname expect:成功-----'
        self.log.info(text)
        select_sql = f"select pg_class.relname from " \
            f"pg_class,pg_partition where " \
            f"pg_class.oid=pg_partition.reltoastrelid and pg_class.oid in " \
            f"(select a.reltoastrelid from pg_partition a,(select oid from " \
            f"pg_partition where parentid = (select oid from pg_class where " \
            f"relname = '{self.t_name}')) b where a.parentid = b.oid order " \
            f"by a.relname) and (pg_partition.relname='p_range_1_1' or " \
            f"pg_partition.relname='p_range_1_2' ) " \
            f"order by pg_class.relname;"
        msg = self.pri_sh.execut_db_sql(select_sql)
        self.log.info(msg)
        self.assertIn('2 rows', msg, '执行失败:' + text)
        relname1 = msg.splitlines()[-3]
        relname2 = msg.splitlines()[-2]
        self.log.info(relname1)

        text = '-----step3:插入数据 expect:成功-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(1,1,1,1);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step4:更新数据,执行20次 expect:成功-----'
        self.log.info(text)
        update_sql = f"update {self.t_name} set col_2=col_2||col_2 " \
            f"where col_1=1;"
        for i in range(20):
            msg = self.pri_sh.execut_db_sql(update_sql)
            self.log.info(msg)
            self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg,
                          '执行失败:' + text)

        text = '-----step5:查询分区p_range_1_1的长度和对应的pg_toast数据 ' \
               'expect:成功,数据压缩和行外存储-----'
        self.log.info(text)
        select_sql = f"select length(col_2) from {self.t_name} " \
            f"subpartition(p_range_1_1);" \
            f"select chunk_id,chunk_seq,length(chunk_data) from " \
            f"pg_toast.{relname1};"
        msg = self.pri_sh.execut_db_sql(select_sql)
        self.log.info(msg)
        self.assertIn('1048576', msg, '执行失败:' + text)
        self.assertIn('7 rows', msg, '执行失败:' + text)

        text = '-----step6:清空表数据,设置存储模式external,插入新数据 expect:成功-----'
        self.log.info(text)
        insert_sql = f"truncate {self.t_name};" \
            f"alter table {self.t_name} alter col_2 set storage external;" \
            f"insert into {self.t_name} values(2,2,2,2);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.TRUNCATE_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertIn(self.constant.ALTER_TABLE_MSG, msg, '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step7:更新数据,执行20次 expect:成功-----'
        self.log.info(text)
        update_sql = f"update {self.t_name} set col_2=col_2||col_2 " \
            f"where col_1=2;"
        for i in range(20):
            msg = self.pri_sh.execut_db_sql(update_sql)
            self.log.info(msg)
            self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg,
                          '执行失败:' + text)

        text = '-----step8:查询分区p_range_1_1的长度和对应的pg_toast数据 ' \
               'expect:成功,数据未压缩和行外存储-----'
        self.log.info(text)
        select_sql = f"select length(col_2) from {self.t_name} " \
            f"subpartition(p_range_1_1);" \
            f"select count(length(chunk_data)) from pg_toast.{relname1};"
        msg = self.pri_sh.execut_db_sql(select_sql)
        self.log.info(msg)
        self.assertIn('1048576', msg, '执行失败:' + text)
        self.assertIn('526', msg, '执行失败:' + text)

        text = '-----step9:插入新数据 expect:成功-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(8,8,8,8);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step10:更新数据,执行20次 expect:成功-----'
        self.log.info(text)
        update_sql = f"update {self.t_name} set col_2=col_2||col_2 " \
            f"where col_1=8;"
        for i in range(20):
            msg = self.pri_sh.execut_db_sql(update_sql)
            self.log.info(msg)
            self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg,
                          '执行失败:' + text)

        text = '-----step11:查询分区p_range_1_2的长度和对应的pg_toast数据 ' \
               'expect:成功,数据未压缩和行外存储-----'
        self.log.info(text)
        select_sql = f"select length(col_2) from {self.t_name} " \
            f"subpartition(p_range_1_2);" \
            f"select count(length(chunk_data)) from pg_toast.{relname2};"
        msg = self.pri_sh.execut_db_sql(select_sql)
        self.log.info(msg)
        self.assertIn('1048576', msg, '执行失败:' + text)
        self.assertIn('526', msg, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step12:清理环境-----')
        text_1 = '-----删除表和表空间 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop tablespace if exists {self.ts_name};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
